/* Copyright (C) 2011 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#ifndef TRI_PHYSICS_COMMON_H__
#define TRI_PHYSICS_COMMON_H__

#include <stdlib.h>
#include <iostream>

#include <Newton.h>

#include "../base/matrix.h"
#include "../entity.h"
#include "../base/threads.h"


// this is the call back for allocation newton memory
void* TAllocMemory (int sizeInBytes);

// this is the callback for freeing Newton Memory
void TFreeMemory (void *ptr, int sizeInBytes);

void TDestroyBodyCallback (const NewtonBody* body);

// Transform callback to set the matrix of a the visual entity
void TSetTransformCallback (const NewtonBody* body, const dFloat* matrix, int threadIndex);

// callback to apply external forces to body
void TApplyForceAndTorqueCallback (const NewtonBody* body, dFloat timestep, int threadIndex);

//create a new material type
//Returns a material ID. 
int TGenMaterialGroup(const NewtonWorld* newtonWorld);

void TRegisterCollisionsType(NewtonWorld* newtonWorld, int mat1, int mat2, void* meta, void (*GenericContactProcess) (const NewtonJoint*, dFloat, int));

namespace tri{

class TPhysicsUpdateThread : public tri::TThread {
public:
	TPhysicsUpdateThread(NewtonWorld* world);
	virtual ~TPhysicsUpdateThread();

	virtual void run();
	void setUpdateRecursion(unsigned int);
	
private:
	NewtonWorld* m_NewtonWorld;
	unsigned int m_UpdateRecursion;
};

}

#endif
